package org.eduapp.trains.web.action.home;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.eduapp.trains.ServiceException;
import org.eduapp.trains.model.announce.ModelAnnounce;
import org.eduapp.trains.service.announce.AnnounceService;
import org.eduapp.trains.web.action.BaseAction;

public class HomeAction extends BaseAction {
	
	private final static Logger logger = Logger.getLogger(HomeAction.class);
	private AnnounceService announceService;
	
	/**
	 * 
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 */
	public ActionForward homeIndex (ActionMapping mapping,
			ActionForm form, HttpServletRequest request,
			HttpServletResponse response) {

		try {
			final int PAGESIZE=13;
			int offset=0;
			String page = request.getParameter("page");
			if(page!=null&&!page.equalsIgnoreCase("")){
				offset = (Integer.parseInt(page)-1)*PAGESIZE;
			}
			ModelAnnounce model=new ModelAnnounce();
			request.setAttribute("gg", announceService.get("1"));
			request.setAttribute("xw", announceService.get(model, PAGESIZE, offset, "announceLastModifyTime", true));
		} catch (ServiceException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		request.setAttribute("nav_code", "home");
		
		return mapping.findForward("home.index");
	}
	
	
	/**
	 * 
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 */
	public ActionForward newsDetail (ActionMapping mapping,
			ActionForm form, HttpServletRequest request,
			HttpServletResponse response) {
		try {
			//final int PAGESIZE=4;
			//int offset=0;
			//ModelAnnounce model=new ModelAnnounce();
			String id = request.getParameter("id");
			request.setAttribute("gg", announceService.get("1"));
			request.setAttribute("news", announceService.get(id));
			//request.setAttribute("xw", announceService.get(model, PAGESIZE, offset, "announceLastModifyTime", true));
		} catch (ServiceException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		//request.setAttribute("nav_code", "home");
		
		return mapping.findForward("home.news");
	}
	
	

	public static Logger getLogger() {
		return logger;
	}

	public AnnounceService getAnnounceService() {
		return announceService;
	}

	public void setAnnounceService(AnnounceService announceService) {
		this.announceService = announceService;
	}
	
}
